//
//  WIFIManagerTableViewCell.m
//  Router
//
//  Created by teki on 15/11/3.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "WIFIManagerTableViewCell.h"
#import "StationModel.h"

@interface WIFIManagerTableViewCell ()
@property (nonatomic, strong) UIImageView *iconImageView; /// 标记是否是母猫还是子猫
@property (nonatomic, strong) UILabel *deviceLabel;
@property (nonatomic, strong) UIImageView *signalImageView;
@property (nonatomic, strong) UIImageView *highImageView;
@property (nonatomic, strong) UIImageView *arrowImageView;

@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) UILabel *statusLabel;

@end

@implementation WIFIManagerTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        if ([reuseIdentifier isEqualToString:@"WIFIManagerTableViewCell"]) {
            [self initManagerUI];
        }
        else{
            [self initUpgradeUI];
        }

    }
    return self;
}


- (void)initUpgradeUI
{
    CGFloat leftPadding = 22;
    CGFloat rightPadding = 15;
    CGFloat viewHorizontalPadding = 10;
    
    self.backgroundColor = [UIColor clearColor];
    
    UIImage *image = [UIImage imageNamed:@"home_tree"];
    self.iconImageView = [self.contentView addImageViewWith:image];
    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftPadding);
        make.centerY.equalTo(self.contentView);
        make.size.equalTo(image.size);
    }];
    
    
    self.deviceLabel = [self.contentView addLabelWithText:@"63000000000" font:[UIFont systemFontOfSize:16.0f] color:NavgationBarBackgroundColor];
    [self.deviceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.right).offset(viewHorizontalPadding);
        make.centerY.equalTo(self.contentView);
    }];
    
    
    self.statusLabel = [self.contentView addLabelWithText:@"" font:[UIFont systemFontOfSize:14.0f] color:[UIColor blackColor]];
    [self.statusLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.offset(-rightPadding);
    }];
    
    
    self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.contentView addSubview:self.indicatorView];
    [self.indicatorView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.statusLabel.left).offset(-rightPadding);
    }];
    
}

- (void)initManagerUI
{
    UIColor *bgColor = [UIColor whiteColor];
    CGFloat leftPadding = 22;
    CGFloat rightPadding = 15;
    CGFloat viewHorizontalPadding = 10;
    
    self.backgroundColor = bgColor;
    
    UIImage *image = [UIImage imageNamed:@"home_tree"];
    self.iconImageView = [self.contentView addImageViewWith:image];
    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftPadding);
        make.centerY.equalTo(self.contentView);
        make.size.equalTo(image.size);
    }];
    
    
    self.deviceLabel = [self.contentView addLabelWithText:@"63000000000" font:[UIFont systemFontOfSize:16.0f] color:NavgationBarBackgroundColor];
    [self.deviceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.right).offset(viewHorizontalPadding);
        make.centerY.equalTo(self.contentView);
    }];
    
    image = [UIImage imageNamed:@"home_signal0"];
    self.signalImageView = [self addImageViewWith:image];
    [self.signalImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.deviceLabel.right).offset(viewHorizontalPadding);
        make.centerY.equalTo(self.contentView);
        //make.size.equalTo(image.size);
        make.height.mas_equalTo(self.contentView.height).offset(-10);
        make.width.mas_equalTo(self.contentView.height).offset(-10);
    }];
    
    
    image = [UIImage imageNamed:@"home_signal_high"];
    self.highImageView = [self.contentView addImageViewWith:image];
    [self.highImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.signalImageView.right).offset(viewHorizontalPadding);
        make.centerY.equalTo(self.contentView);
        make.size.equalTo(image.size);
    }];
    self.highImageView.hidden = YES;
    
    image = [UIImage imageNamed:@"home_list_pre"];
    self.arrowImageView = [self.contentView addImageViewWith:image];
    [self.arrowImageView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-rightPadding);
        make.centerY.equalTo(self.contentView);
        make.size.equalTo(image.size);
    }];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)setStationModel:(StationModel *)stationModel
{
    _stationModel = stationModel;
    if (stationModel.master)
    {
        self.iconImageView.image = [UIImage imageNamed:@"home_tree"];
        self.signalImageView.hidden = YES;
    }
    else
    {
        self.signalImageView.hidden = NO;
        self.iconImageView.image = [UIImage imageNamed:@"home_leaf"];
        [self setSignalImageValue:stationModel.link_quality];
    }
    self.deviceLabel.text = stationModel.alias;
}


- (void)setUpgradeModel:(StationModel *)upgradeModel
{
//    [_upgradeModel removeObserver:self forKeyPath:@"_type"];
    _upgradeModel = upgradeModel;
    
    self.deviceLabel.textColor =  NavgationBarBackgroundColor;
    if (upgradeModel.master)
    {
        if (upgradeModel.wifi_enable)
        {
            self.iconImageView.image = [UIImage imageNamed:@"home_tree"];

        }
        else{
            self.iconImageView.image = [UIImage imageNamed:@"home_tree_light"];
            self.deviceLabel.textColor =   RGB(159, 161, 154);
        }

    }
    else
    {
        if (upgradeModel.wifi_enable)
        {
            self.iconImageView.image = [UIImage imageNamed:@"home_leaf"];

        }
        else{
            self.iconImageView.image = [UIImage imageNamed:@"home_leaf_light"];
            self.deviceLabel.textColor =   RGB(159, 161, 154);
        }

    }
    self.deviceLabel.text = upgradeModel.alias;

    //    [_upgradeModel addObserver:self forKeyPath:@"_type" options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew) context:nil];
    
    [self setStatus:upgradeModel.type];
    
    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"_type"]) {
        [self setStatus:[change[@"new"] integerValue]];
    }
}


- (void)setStatus:(UpgradeType)status
{
    if (status == UpgradeTypeHasNewVersion) {
        [self.indicatorView stopAnimating];
        self.indicatorView.hidden = YES;
        self.statusLabel.text = @"发现新版固件";
        self.statusLabel.textColor =   RGB(214, 138, 67);
    }
    else if (status == UpgradeTypeNewest){
        [self.indicatorView stopAnimating];
        self.indicatorView.hidden = YES;
        self.statusLabel.text = @"已是最新固件";
        self.statusLabel.textColor =   RGB(134, 174, 124);

    }
    else if (status == UpgradeTypeOffLine)
    {
        [self.indicatorView stopAnimating];
        self.indicatorView.hidden = YES;
        self.statusLabel.text = @"离线";
        self.statusLabel.textColor =   RGB(159, 161, 154);

    }
    else if (status == UpgradeTypeScaning)
    {
        [self.indicatorView stopAnimating];
        self.indicatorView.hidden = NO;
        [self.indicatorView startAnimating];
        self.statusLabel.text = @"扫描中";
        self.statusLabel.textColor =   RGB(159, 161, 154);
    }else{
        [self.indicatorView stopAnimating];
        self.indicatorView.hidden = YES;
        self.statusLabel.text = @"";
    }

}
#pragma mark -
#pragma mark   PrivateMethods
- (void)setSignalImageValue:(CGFloat)value
{
    UIImage *image = [UIImage imageNamed:@"home_signal0"];
    if (value >=70)
    {
        image = [UIImage imageNamed:@"home_signal3"];
    }
    else if (value >= 40 && value <70)
    {
        image = [UIImage imageNamed:@"home_signal2"];
    }
    else if (value >=10 && value < 40){
        image = [UIImage imageNamed:@"home_signal1"];
    }
    self.signalImageView.image = image;
}

@end
